package com.caigou.controller;

import com.caigou.annotation.OperLog;
import com.caigou.bean.cms.param.*;
import com.caigou.bean.exam.entity.Users;
import com.caigou.component.HtmlBuilderComponent;
import com.caigou.constant.NewsConstant;
import com.caigou.enums.ResponseEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.GovProListService;
import com.caigou.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/govProList/")
public class GovProListController {

    @Resource
    private HtmlBuilderComponent htmlBuilderComponent;
    @Resource
    private GovProListService govProListService;

    @RequestMapping("listIntro")
    @PreAuthorize("hasAnyAuthority('chakanpindaolanmuneirong')")
    public Result listIntro(@CurrentUser Users user){
        return govProListService.listIntro();
    }

    /**
     * 增加政采榜介绍内容组
     * @param param
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "增加政采榜介绍内容组")
    @RequestMapping("addIntroGroup")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result addIntroGroup(@RequestBody GovProListIntroGroupParam param, @CurrentUser Users user){
        if(param.getGroupName()!=null && param.getGroupName().length()>20){
            return Result.error("政采榜内容标题不能超过20个字", "政采榜内容标题不能超过20个字");
        }
        return govProListService.saveIntroGroup(param);
    }

    /**
     * 删除政采榜介绍内容组
     * @param param
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除政采榜介绍内容组")
    @RequestMapping("deleteIntroGroup")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result deleteIntroGroup(@RequestBody GovProListIntroGroupParam param, @CurrentUser Users user){
        return govProListService.deleteIntroGroup(param);
    }

    /**
     * 更新政采榜介绍内容组
     * @param param
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "更新政采榜介绍内容组")
    @RequestMapping("updateIntroGroup")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updateIntroGroup(@RequestBody GovProListIntroGroupParam param, @CurrentUser Users user){
        if(param.getGroupName()!=null && param.getGroupName().length()>20){
            return Result.error("政采榜内容标题不能超过20个字", "政采榜内容标题不能超过20个字");
        }
        return govProListService.updateIntroGroup(param);
    }

    /**
     * 增加政采榜介绍段落
     * @param param
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "增加政采榜介绍段落")
    @RequestMapping("saveIntroItem")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result saveIntroItem(@RequestBody GovProListIntroItemParam param, @CurrentUser Users user){
        if(param.getItemContent()!=null && param.getItemContent().length()>256){
            return Result.error("政采榜段落内容不能超过256个字", "政采榜段落内容不能超过256个字");
        }
        return govProListService.saveIntroItem(param);
    }

    /**
     * 删除政采榜介绍内容段落
     * @param param
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除政采榜介绍内容段落")
    @RequestMapping("deleteIntroItem")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result deleteIntroItem(@RequestBody GovProListIntroItemParam param, @CurrentUser Users user){
        return govProListService.deleteIntroItem(param);
    }

    /**
     * 更新政采榜介绍内容段落
     * @param param
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "更新政采榜介绍内容段落")
    @RequestMapping("updateIntroItem")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updateIntroItem(@RequestBody GovProListIntroItemParam param, @CurrentUser Users user){
        if(param.getItemContent()!=null && param.getItemContent().length()>256){
            return Result.error("政采榜段落内容不能超过256个字", "政采榜段落内容不能超过256个字");
        }
        return govProListService.updateIntroItem(param);
    }


    /**
     * 发布政采榜介绍，即生成html静态页面碎片
     * @return
     */
    @RequestMapping("publishIntro")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result publishIntro(@CurrentUser Users user){
        String url = govProListService.buildIntro();
        return Result.success(url);
    }

    /**
     * 增加政采榜奖项
     * @param param
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "增加政采榜奖项")
    @RequestMapping("savePrizeSum")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result savePrizeSum(@RequestBody GovProListPrizeSumParam param, @CurrentUser Users user){
        if(param.getPrizeName()!=null && param.getPrizeName().length()>32){
            return Result.error("政采榜奖项名称不能超过32个字", "政采榜奖项名称不能超过32个字");
        }
        if(param.getPrizeUrl()!=null && param.getPrizeUrl().length()>128){
            return Result.error("政采榜奖项链接不能超过128个字", "政采榜奖项链接不能超过128个字");
        }
        return govProListService.savePrizeSum(param);
    }

    /**
     * 删除政采榜奖项
     * @param param
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除政采榜奖项")
    @RequestMapping("deletePrizeSum")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result deletePrizeSum(@RequestBody GovProListPrizeSumParam param, @CurrentUser Users user){
        return govProListService.deletePrizeSum(param);
    }

    /**
     * 更新政采榜奖项
     * @param param
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "更新政采榜奖项")
    @RequestMapping("updatePrizeSum")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updatePrizeSum(@RequestBody GovProListPrizeSumParam param, @CurrentUser Users user){
        if(param.getPrizeName()!=null && param.getPrizeName().length()>32){
            return Result.error("政采榜奖项名称不能超过32个字", "政采榜奖项名称不能超过32个字");
        }
        if(param.getPrizeUrl()!=null && param.getPrizeUrl().length()>128){
            return Result.error("政采榜奖项链接不能超过128个字", "政采榜奖项链接不能超过128个字");
        }
        return govProListService.updatePrizeSum(param);
    }

    /**
     * 移动政采榜奖项
     * @param param
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "移动政采榜奖项")
    @RequestMapping("movePrizeSum")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result movePrizeSum(@RequestBody GovProListPrizeSumParam param, @CurrentUser Users user){
        return govProListService.movePrizeSum(param);
    }


    /**
     * 列表政采榜奖项
     * @param user
     * @return
     */
    @RequestMapping("listPrizeSum")
    @PreAuthorize("hasAnyAuthority('chakanpindaolanmuneirong')")
    public Result listPrizeSum(@CurrentUser Users user){
        return govProListService.listPrizeSum();
    }


    /**
     * 发布政采榜奖项，及生成html静态页面碎片
     * @return
     */
    @RequestMapping("publishPrizeSum")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result publishPrizeSum(@CurrentUser Users user){
        String url = govProListService.buildPrizeSum();
        return Result.success(url);
    }

    /**
     * 专家政采榜专家
     * @param param
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "增加政采榜专家")
    @RequestMapping("saveExpert")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result saveExpert(@RequestBody GovProListExpertParam param, @CurrentUser Users user){
        if(param.getExpertName()!=null && param.getExpertName().length()>9){
            return Result.error("政采榜评委专家姓名不能超过9个字", "政采榜评委专家姓名不能超过9个字");
        }
        if(param.getExpertIntro()!=null && param.getExpertIntro().length()>70){
            return Result.error("政采榜评委专家简介不能超过70个字", "政采榜评委专家简介不能超过70个字");
        }
        return govProListService.saveExpert(param);
    }

    /**
     * 删除政采榜专家
     * @param param
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除政采榜专家")
    @RequestMapping("deleteExpert")
    @PreAuthorize("hasAnyAuthority('yichupindaoneirong')")
    public Result deleteExpert(@RequestBody GovProListExpertParam param, @CurrentUser Users user){
        return govProListService.deleteExpert(param);
    }

    /**
     * 更新政采榜专家
     * @param param
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "更新政采榜专家")
    @RequestMapping("updateExpert")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updatePrizeSum(@RequestBody GovProListExpertParam param, @CurrentUser Users user){
        if(param.getExpertName()!=null && param.getExpertName().length()>9){
            return Result.error("政采榜评委专家姓名不能超过9个字", "政采榜评委专家姓名不能超过9个字");
        }
        if(param.getExpertIntro()!=null && param.getExpertIntro().length()>70){
            return Result.error("政采榜评委专家简介不能超过70个字", "政采榜评委专家简介不能超过70个字");
        }
        return govProListService.updateExpert(param);
    }

    /**
     * 移动政采榜专家
     * @param param
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "移动政采榜专家")
    @RequestMapping("moveExpert")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result moveExpert(@RequestBody GovProListExpertParam param, @CurrentUser Users user){
        return govProListService.moveExpert(param);
    }

    /**
     * 列表政采榜专家
     * @param user
     * @return
     */
    @RequestMapping("listExpert")
    @PreAuthorize("hasAnyAuthority('chakanpindaolanmuneirong')")
    public Result listExpert(@CurrentUser Users user){
        return govProListService.listExpert();
    }

    /**
     * 发布政采榜专家，及生成html静态页面碎片
     * @return
     */
    @RequestMapping("publishExpert")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result publishExpert(@CurrentUser Users user){
        String url = govProListService.buildExpert();
        return Result.success(url);
    }

    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "增加奖项年度")
    @RequestMapping("savePrizeYear")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result savePrizeYear(@RequestBody GovProListPrizeYearParam param, @CurrentUser Users user){
        if(param.getYear()!=null && String.valueOf(param.getYear()).length()>4){
            return Result.error("年度不能超过4位", "年度不能超过4位");
        }
        return govProListService.savePrizeYear(param);
    }

    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除奖项年度")
    @RequestMapping("deletePrizeYear")
    public Result deletePrizeYear(@RequestBody GovProListPrizeYearParam param, @CurrentUser Users user){
        return govProListService.deletePrizeYear(param);
    }

    @RequestMapping("listPrizeYear")
    @PreAuthorize("hasAnyAuthority('chakanpindaolanmuneirong')")
    public Result listPrizeYear(@RequestBody GovProListPrizeYearParam param, @CurrentUser Users user){
        return govProListService.listPrizeYear(param);
    }

    @RequestMapping("updatePrizeYear")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updatePrizeYear(@RequestBody GovProListPrizeYearParam param, @CurrentUser Users user){
        if(param.getYear()!=null && String.valueOf(param.getYear()).length()>4){
            return Result.error("年度不能超过4位", "年度不能超过4位");
        }
        return govProListService.updatePrizeYear(param);
    }

    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "增加奖项")
    @RequestMapping("savePrize")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result savePrize(@RequestBody GovProListPrizeParam param, @CurrentUser Users user){
        if(param.getPrizeName()!=null && param.getPrizeName().length()>30){
            return Result.error("奖项名称不能超过30个字", "奖项名称不能超过30个字");
        }
        return govProListService.savePrize(param);
    }

    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除奖项")
    @RequestMapping("deletePrize")
    @PreAuthorize("hasAnyAuthority('yichupindaoneirong')")
    public Result deletePrize(@RequestBody GovProListPrizeParam param, @CurrentUser Users user){
        return govProListService.deletePrize(param);
    }

    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "更新奖项")
    @RequestMapping("updatePrize")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updatePrize(@RequestBody GovProListPrizeParam param, @CurrentUser Users user){
        if(param.getPrizeName()!=null && param.getPrizeName().length()>30){
            return Result.error("奖项名称不能超过30个字", "奖项名称不能超过30个字");
        }
        return govProListService.updatePrize(param);
    }

    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "更新奖项")
    @RequestMapping("movePrize")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result movePrize(@RequestBody GovProListPrizeParam param, @CurrentUser Users user){
        return govProListService.movePrize(param);
    }

    @RequestMapping("listPrize")
    @PreAuthorize("hasAnyAuthority('chakanpindaolanmuneirong')")
    public Result listPrize(@RequestBody GovProListPrizeParam param, @CurrentUser Users user){
        return govProListService.listPrize(param.getPrizeYearId());
    }

    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "更新奖项类型")
    @RequestMapping("updatePrizeType")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updatePrizeType(@RequestBody GovProListPrizeParam param, @CurrentUser Users user){
        return govProListService.updatePrize(param);
    }

    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "增加获奖者")
    @RequestMapping("savePrizeWinner")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result savePrizeWinner(@RequestBody GovProListPrizeWinnerParam param, @CurrentUser Users user){
        if(param.getWinnerName()!=null && param.getWinnerName().length()>30){
            return Result.error("个人或者单位名称不能超过30个字", "个人或者单位名称不能超过30个字");
        }
        if(param.getWinnerPosition()!=null && param.getWinnerPosition().length()>80){
            return Result.error("职位不能超过80个字", "职位不能超过80个字");
        }
        if(param.getWinnerIntro()!=null && param.getWinnerIntro().length()>90){
            return Result.error("介绍不能超过90个字", "介绍不能超过90个字");
        }
        return govProListService.savePrizeWinner(param);
    }

    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除获奖者")
    @RequestMapping("deletePrizeWinner")
    @PreAuthorize("hasAnyAuthority('yichupindaoneirong')")
    public Result deletePrizeWinner(@RequestBody GovProListPrizeWinnerParam param, @CurrentUser Users user){
        return govProListService.deletePrizeWinner(param);
    }

    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "更新获奖者")
    @RequestMapping("updatePrizeWinner")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updatePrizeWinner(@RequestBody GovProListPrizeWinnerParam param, @CurrentUser Users user){
        if(param.getWinnerName()!=null && param.getWinnerName().length()>30){
            return Result.error("个人或者单位名称不能超过30个字", "个人或者单位名称不能超过30个字");
        }
        if(param.getWinnerPosition()!=null && param.getWinnerPosition().length()>80){
            return Result.error("职位不能超过80个字", "职位不能超过80个字");
        }
        if(param.getWinnerIntro()!=null && param.getWinnerIntro().length()>90){
            return Result.error("介绍不能超过90个字", "介绍不能超过90个字");
        }
        return govProListService.updatePrizeWinner(param);
    }

    @OperLog(operModule = NewsConstant.GOV_PRO_LIST_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "移动获奖者")
    @RequestMapping("movePrizeWinner")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result movePrizeWinner(@RequestBody GovProListPrizeWinnerParam param, @CurrentUser Users user){
        return govProListService.movePrizeWinner(param);
    }

    @RequestMapping("listPrizeWinner")
    @PreAuthorize("hasAnyAuthority('chakanpindaolanmuneirong')")
    public Result listPrizeWinner(@RequestBody GovProListPrizeWinnerParam param, @CurrentUser Users user){
        return govProListService.listPrizeWinner(param.getPrizeId());
    }

    @RequestMapping("getGovProListOtherConfig")
    @PreAuthorize("hasAnyAuthority('chakanpindaolanmuneirong')")
    public Result getGovProListOtherConfig(@RequestBody GovProListOtherConfigParam param, @CurrentUser Users user){
        return govProListService.getOtherConfigByChannelValue(param.getChannelValue());
    }

    @RequestMapping("updateGovProListOtherConfig")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updateGovProListOtherConfig(@RequestBody GovProListOtherConfigParam param, @CurrentUser Users user){
        if(param.getChannelTitle()!=null && param.getChannelTitle().length()>64){
            return Result.error("频道名称不能超过64个字", "频道名称不能超过64个字");
        }
        if(param.getChannelKeyword()!=null && param.getChannelKeyword().length()>64){
            return Result.error("页面关键词不能超过64个字", "页面关键词不能超过64个字");
        }
        if(param.getChannelDescriptions()!=null && param.getChannelDescriptions().length()>300){
            return Result.error("频道介绍不能超过300个字","频道介绍不能超过300个字");
        }
        return govProListService.updateOtherConfig(param);
    }

    @RequestMapping("publishHomePage")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result publishHomePage(@CurrentUser Users user){
        String url0 = govProListService.buildGovFooter();
        String url = govProListService.buildHomePage();
        return Result.success(url+","+url0);
    }

    @RequestMapping("publishGovWinner")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result publishGovWinner(@CurrentUser Users user){
        String url = govProListService.buildGovWinner();
        return Result.success(url);
    }

    @RequestMapping("publishEnterpriseWinner")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result publishEnterpriseWinner(@CurrentUser Users user){
        String url = govProListService.buildEnterpriseWinner();
        return Result.success(url);
    }
}
